package com.cheney.weiboo.test.view;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.color;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.cheney.weiboo.R;
import com.cheney.weiboo.domain.Users;
import com.cheney.weiboo.service.UserService;
import com.cheney.weiboo.utils.WeiboContext;
import com.mime.qweibo.examples.MyWeiboSync;

public class AddWeiBoActivity extends Activity implements OnClickListener{
	
	private ImageView addPic;
	private ImageView addAt;
	private ImageView addTopic;
	private ImageView addEmo;
	private ImageView location;
	private Button send;
	private MyWeiboSync weibo;
	private Handler handler;
	private String userId;
	private UserService userService;
	private RelativeLayout add_weibo_head;
	private EditText content;
	private Users user;
	private RelativeLayout relativeLayout;
	private EditText atEditText;
	private Button atButton;
	private ListView atListView;
	private RelativeLayout.LayoutParams atEditLayoutParams,atButtonLayoutParams,listViewLayoutParams;
	private ExecutorService executorService;
	private int THREAD_SIZE = 3;
	private JSONObject jsonObject;
	private JSONArray array;
	private List<String> atList;
	private List<String> matchList;
	private ArrayAdapter<String> arrayAdapter;
	private FrameLayout operation_layout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addweibo);
		setUpView();
		setUpListeners();
		weibo = WeiboContext.getInstance();
		SharedPreferences sp = getSharedPreferences("default_users",Activity.MODE_PRIVATE);
		userId = sp.getString("user_default_name", "");
		Log.d("userId", ""+userId);
		userService = new UserService(getApplicationContext());
		handler = new addWeiboHandler();
		executorService.submit(new AtThread());
		
	}
	//设置VIEW
	public void setUpView(){
		addPic = (ImageView)findViewById(R.id.add_pic);
		addAt = (ImageView)findViewById(R.id.add_at);
		addTopic = (ImageView)findViewById(R.id.add_topic);
		addEmo = (ImageView)findViewById(R.id.add_emo);
		location = (ImageView)findViewById(R.id.add_location);
		send = (Button)findViewById(R.id.send);
		content = (EditText)findViewById(R.id.addweibo_content);
		operation_layout = (FrameLayout)findViewById(R.id.operation_layout);
		add_weibo_head = (RelativeLayout)findViewById(R.id.addweibo_head);
		
		executorService = Executors.newFixedThreadPool(THREAD_SIZE);
		atList = new ArrayList<String>();
		
		//动态的创建布局结构
		relativeLayout = new RelativeLayout(AddWeiBoActivity.this);
		//创建一个EditText
		atEditText = new EditText(AddWeiBoActivity.this);		
		atEditText.setId(10000);
		
		atButton = new Button(AddWeiBoActivity.this);
		atButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_enter_selector));
		
		atListView = new ListView(AddWeiBoActivity.this);
		atListView.setCacheColorHint(color.transparent);
		atListView.setDivider(getResources().getDrawable(R.drawable.list_divider));
		atListView.setBackgroundColor(Color.argb(255, 239, 239, 239));
		
		relativeLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		atEditLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		atButtonLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		listViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		
		atEditLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		
		atButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
		atButtonLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		atButtonLayoutParams.setMargins(0, 0, 10, 0);
		
		listViewLayoutParams.addRule(RelativeLayout.BELOW,atEditText.getId());
		listViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		
		atEditText.setLayoutParams(atEditLayoutParams);
		atButton.setLayoutParams(atButtonLayoutParams);
		atListView.setLayoutParams(listViewLayoutParams);
	}
	
	//创建handler
	class addWeiboHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			int size = array.length();
			for(int i=0;i<size;i++){
				JSONObject data = array.optJSONObject(i);
				try {
					atList.add(data.getString("nick")+"("+data.getString("name")+")");
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			Log.d("listSize", ""+atList.size());
			arrayAdapter = new ArrayAdapter<String>(AddWeiBoActivity.this,R.layout.at_item,R.id.at_nick_name,atList);
			atListView.setAdapter(arrayAdapter);
		}
	}
	
	//设置Listeners
	public void setUpListeners(){
		send.setOnClickListener(this);
		addAt.setOnClickListener(this);
	}
	
	class AtThread extends Thread{
		@Override
		public void run() {
			String str = weibo.getFans(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), 50, 0, userId);
			Log.d("Fans", ""+str);
			try {
				 jsonObject = new JSONObject(str).getJSONObject("data");
				 array = jsonObject.getJSONArray("info");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			 Message message = handler.obtainMessage();
			 handler.sendMessage(message);
		}
		
	}
	
	
	@Override
	public void onClick(View view) {
		switch(view.getId()){
		//点击发送按钮
		case R.id.send : {
			String returnStr = null;
			returnStr = weibo.publishMsg(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), content.getText().toString());
			try {
				JSONObject jsonObject = new JSONObject(returnStr);
				if("ok".equals(jsonObject.get("msg"))){
					Toast.makeText(AddWeiBoActivity.this,"发布成功", Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Log.d("发送按钮", ""+returnStr);
		}
		break;
		case R.id.add_at :{
			//删除Layout上面的所有VIEW
			Log.d("@", "@");
			relativeLayout.removeAllViews();
			atEditText.setText("@");
			relativeLayout.addView(atEditText);
			relativeLayout.addView(atButton);
			relativeLayout.addView(atListView);
			operation_layout.addView(relativeLayout);
			
			add_weibo_head.setVisibility(View.GONE);
			content.setVisibility(View.GONE);
			
			operation_layout.setVisibility(View.VISIBLE);
		}
		break;
		}
	}
	
	
	
}
